<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Summary of Joint Observations by SVOM and GWAC</title>
    <style>
        .image-grid {
            display: flex;
            flex-wrap: wrap;
            gap: 10px;
            justify-content: center;
        }
        .image-grid img {
            width: 300px;
            height: auto;
        }
        .content-container {
            text-align: center;
        }
        .pagination {
            margin-top: 20px;
            margin-bottom: 20px;
        }
        .pagination button {
            margin: 0 5px;
            padding: 5px 10px;
            cursor: pointer;
        }
        .pagination button.active {
            background-color: #007bff;
            color: white;
        }
    </style>
</head>
<body style="text-align: center;">
    <h1>Summary of Joint Observations by SVOM and GWAC</h1>

    <!-- 分页按钮（页首） -->
    <div class="pagination">
        <button id="prev-top">上一页</button>
        <span id="page-numbers-top"></span>
        <button id="next-top">下一页</button>
    </div>

    <div id="content"></div>

    <!-- 分页按钮（页尾） -->
    <div class="pagination">
        <button id="prev-bottom">上一页</button>
        <span id="page-numbers-bottom"></span>
        <button id="next-bottom">下一页</button>
    </div>

    <script>
        const pageSize = 5;
        let page = 0;
        let dates = [];

        async function fetchDates() {
            const response = await fetch('/gwacWebHost/api/dates');
            dates = await response.json();
            renderPage();
            updatePagination();
        }

        async function fetchData(date) {
            const response = await fetch(`/gwacWebHost/api/data/${date}`);
            return response.json();
        }

        async function renderPage() {
            const container = document.getElementById('content');
            container.innerHTML = '';
            const start = page * pageSize;
            const end = start + pageSize;
            const pageDates = dates.slice(start, end);

            for (const date of pageDates) {
                const data = await fetchData(date);
                const section = document.createElement('div');
                section.classList.add('content-container');
                section.innerHTML = `
                    <h2>${date}</h2>
                    <p>TBD: More Info to Display...</p>
                    <img src="/gwacWebHost/svomSummary/data/${date}/${data.overlap_ratios_image}" width="1200">
                    <p>Sky Coverage Images: <br/>
                        随机采样5个覆盖率不足100%的时刻和5个覆盖率达到100%的时刻，每个时刻绘制一幅图像。<br/>
                        红色是SVOM的天区，蓝色是地平高度大于30度的区域，绿色是GWAC的观测天区。</p>
                    <div class="image-grid">
                        ${data.sky_coverage_images.sort().map(img => `<img src="/gwacWebHost/svomSummary/data/${date}/${img}">`).join('')}
                    </div>
                `;
                container.appendChild(section);
            }
        }

        function updatePagination() {
            const totalPages = Math.ceil(dates.length / pageSize);
            const startPage = Math.max(1, page - 2);
            const endPage = Math.min(totalPages, page + 3);

            // 更新页首分页按钮
            updatePaginationSection('page-numbers-top', startPage, endPage, totalPages);
            // 更新页尾分页按钮
            updatePaginationSection('page-numbers-bottom', startPage, endPage, totalPages);
        }

        function updatePaginationSection(sectionId, startPage, endPage, totalPages) {
            const pageNumbers = document.getElementById(sectionId);
            pageNumbers.innerHTML = '';

            if (startPage > 1) {
                const firstPageButton = document.createElement('button');
                firstPageButton.innerText = '1';
                firstPageButton.addEventListener('click', () => {
                    page = 0;
                    renderPage();
                    updatePagination();
                });
                pageNumbers.appendChild(firstPageButton);
                if (startPage > 2) {
                    const ellipsis = document.createElement('span');
                    ellipsis.innerText = '...';
                    pageNumbers.appendChild(ellipsis);
                }
            }

            for (let i = startPage; i <= endPage; i++) {
                const pageButton = document.createElement('button');
                pageButton.innerText = i;
                if (i - 1 === page) {
                    pageButton.classList.add('active');
                }
                pageButton.addEventListener('click', () => {
                    page = i - 1;
                    renderPage();
                    updatePagination();
                });
                pageNumbers.appendChild(pageButton);
            }

            if (endPage < totalPages) {
                if (endPage < totalPages - 1) {
                    const ellipsis = document.createElement('span');
                    ellipsis.innerText = '...';
                    pageNumbers.appendChild(ellipsis);
                }
                const lastPageButton = document.createElement('button');
                lastPageButton.innerText = totalPages;
                lastPageButton.addEventListener('click', () => {
                    page = totalPages - 1;
                    renderPage();
                    updatePagination();
                });
                pageNumbers.appendChild(lastPageButton);
            }
        }

        // 绑定页首分页按钮事件
        document.getElementById('prev-top').addEventListener('click', () => {
            if (page > 0) {
                page--;
                renderPage();
                updatePagination();
            }
        });
        document.getElementById('next-top').addEventListener('click', () => {
            if ((page + 1) * pageSize < dates.length) {
                page++;
                renderPage();
                updatePagination();
            }
        });

        // 绑定页尾分页按钮事件
        document.getElementById('prev-bottom').addEventListener('click', () => {
            if (page > 0) {
                page--;
                renderPage();
                updatePagination();
            }
        });
        document.getElementById('next-bottom').addEventListener('click', () => {
            if ((page + 1) * pageSize < dates.length) {
                page++;
                renderPage();
                updatePagination();
            }
        });

        document.addEventListener('DOMContentLoaded', fetchDates);
    </script>
</body>
</html>